import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:high_help_apps/common/values/colors.dart';
import 'package:high_help_apps/common/widgets/common/builder_grid_view.dart';
import 'package:high_help_apps/common/widgets/common/common_listtile.dart';
import 'package:high_help_apps/common/widgets/common/common_text_button.dart';
import 'package:high_help_apps/common/widgets/common/normal_divider.dart';
import 'life_help_add_shop_logic.dart';
import 'life_help_add_shop_state.dart';

class LifeHelpAddShopPage extends StatefulWidget {
  @override
  _LifeHelpAddShopPageState createState() => _LifeHelpAddShopPageState();
}

class _LifeHelpAddShopPageState extends State<LifeHelpAddShopPage> {
  final LifeHelpAddShopLogic logic = Get.put(LifeHelpAddShopLogic());
  final LifeHelpAddShopState state = Get.find<LifeHelpAddShopLogic>().state;

  late TextEditingController _nameController;
  late TextEditingController _prepriceController;
  late TextEditingController _afterpriceController;
  late TextEditingController _detailController;
  late TextEditingController _telephoneController;
  late DateTime _selectedDate;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _nameController = TextEditingController();
    _prepriceController = TextEditingController();
    _afterpriceController = TextEditingController();
    _detailController = TextEditingController();
    _telephoneController = TextEditingController();
    _selectedDate = DateTime.now();
  }

  Future<void> _selectDate() async {
    final DateTime? date = await showDatePicker(
      context: context,
      initialDate: _selectedDate,
      firstDate: DateTime(1900),
      lastDate: DateTime(2100),
    );
    print(date);
    if (date == null) return;

    setState(() {
      _selectedDate = date;
    });
  }

  @override
  Widget build(BuildContext context) {
    Widget _getSecondHandShopInput(
            TextEditingController controller, String placeHolder,
            {int maxLines = 1}) =>
        TextField(
          controller: controller,
          maxLines: maxLines,
          cursorColor: AppColors.primaryElement,
          style: TextStyle(fontSize: 14.sp),
          decoration: InputDecoration(
              hintText: placeHolder,
              hintStyle:
                  TextStyle(color: AppColors.subTextColor67, fontSize: 14.sp),
              border: InputBorder.none),
        );

    Widget _buildBottomQuerySheet() => Container(
          color: Colors.white,
          child: ListView(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            padding: EdgeInsets.zero,
            children: [
              CommonListTile(
                onPressed: logic.uploadLocalImage,
                title: '上传本地',
                suffixWidget: Icon(Icons.cloud_upload_outlined),
              ),
              Divider(),
              CommonListTile(
                onPressed: logic.uploadCameraImage,
                title: '拍照上传',
                suffixWidget: Icon(Icons.camera),
              ),
              Divider(),
              CommonListTile(
                onPressed: () {
                  Get.back();
                },
                title: '取消',
                suffixWidget: Icon(Icons.cancel),
              )
            ],
          ),
        );

    Widget _buildAvatarUpload() => Column(
          children: [
            CommonListTile(
              onPressed: () {
                Get.bottomSheet(_buildBottomQuerySheet());
              },
              title: '上传商品图片',
              suffixWidget: Padding(
                padding: EdgeInsets.only(bottom: 3.r),
                child: GetBuilder<LifeHelpAddShopLogic>(
                  builder: (logic) => logic.state.imagePath!.isEmpty
                      ? CircleAvatar(
                          backgroundColor: Colors.black12,
                          child: Padding(
                            padding: EdgeInsets.all(8.r),
                            child: Image.asset(
                              'assets/images/common/add.png',
                            ),
                          ),
                          radius: 25.r,
                        )
                      : Text(
                          logic.state.imagePath!.length.toString() + '张',
                          style: TextStyle(color: AppColors.subTextColor),
                        ),
                ),
              ),
            ),
            GetBuilder<LifeHelpAddShopLogic>(
                builder: (logic) => Stack(
                      children: [
                        BuilderGridView(
                          isShowTitle: false,
                          length: logic.state.imagePath!.length,
                          builder: (BuildContext context, int index) {
                            return GestureDetector(
                              child: Container(
                                decoration: BoxDecoration(
                                    image: DecorationImage(
                                        image: FileImage(File(logic
                                            .state.imagePath![index].path)))),
                              ),
                              onLongPress: () =>
                                  logic.showDeleteImageDialog(context, index),
                            );
                          },
                          crossCount: 4,
                        ),
                        logic.isUploading == true
                            ? Center(
                                child: CircularProgressIndicator(
                                  backgroundColor: AppColors.lightTextColor2,
                                  valueColor: AlwaysStoppedAnimation(
                                      AppColors.primaryElement),
                                  value: logic.currentProcessValue,
                                ),
                              )
                            : Container()
                      ],
                    ))
          ],
        );
    // CircleAvatar(
    //   backgroundColor: Colors.black12,
    //   backgroundImage: FileImage(
    //     File(logic.state.imagePath!.path),
    //   ),
    //   radius: 25.r,
    // ),

    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        title: Text(
          "发布二手商品",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Container(
        padding: EdgeInsets.only(top: 20.w, left: 15.w, right: 15.w),
        child: ListView(
          children: <Widget>[
            Row(
              children: [
                Expanded(
                  child: Text('商品名称',
                      style: TextStyle(color: AppColors.subTextColor)),
                  flex: 1,
                ),
                Expanded(
                  child: _getSecondHandShopInput(_nameController, '发布的二手商品名称'),
                  flex: 3,
                )
              ],
            ),
            NormalDivider(),
            Row(
              children: [
                Expanded(
                  child: Text('原价',
                      style: TextStyle(color: AppColors.subTextColor)),
                  flex: 1,
                ),
                Expanded(
                  child: _getSecondHandShopInput(_prepriceController, '商品原价'),
                  flex: 3,
                ),
                Expanded(
                  child: Text('现价',
                      style: TextStyle(color: AppColors.subTextColor)),
                  flex: 1,
                ),
                Expanded(
                  child: _getSecondHandShopInput(_afterpriceController, '商品现价'),
                  flex: 3,
                )
              ],
            ),
            NormalDivider(),
            _getSecondHandShopInput(_telephoneController, '手机号'),
            NormalDivider(),
            _getSecondHandShopInput(_detailController, '请输入商品的详细信息',
                maxLines: 10),
            NormalDivider(),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 20.h),
              child: _buildAvatarUpload(),
            ),
            CommonTextButton(
              padding: EdgeInsets.only(top: 45.h),
              border: Border.all(color: AppColors.subTextColor12, width: 2.r),
              onPressed: () => logic.releaseSecondCommodity(),
              borderRadius: 12.r,
              title: '发布',
              textColor: Colors.white,
              backgroundColor: AppColors.primaryElement,
            ),
            SizedBox(
              height: 20.h,
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<LifeHelpAddShopLogic>();

    _nameController.dispose();
    _prepriceController.dispose();
    _afterpriceController.dispose();
    _detailController.dispose();

    super.dispose();
  }

}
